<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\OrderTemporary;
use app\common\model\Product as ProductModel;
use app\common\model\ProductSpec;
use app\common\model\ProductCate;
use app\common\model\User;
use app\common\tool\Result;
use think\Response;

class Product extends Base
{
    /**
     * 获取商品分类
     */
    public function getCateList(): Response
    {
        try {
            $list = ProductCate::where(['status' => 1, 'pid' => 0])->order('sort asc')->field('id,title,icon')->select();
            return Result::success('获取成功！', $list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 详情
     */
    public function details($id)
    {
        try {
            $info = ProductModel::where(['status' => 1, 'id' => $id])
                ->field('id,title,price,vip_price,icon,images,content,spec,cate_1,spec_open,stock,sales,sort,status,create_time')
                ->find();
            if (empty($info)) {
                return Result::fail('商品不存在！');
            }
            $info->images = explode(',', $info->images);
            $info->spec_data = $info['spec'] ? json_decode($info['spec'], true) : [];
            $url = 'https://' . $_SERVER['SERVER_NAME'] . '/uploads/';
            $info->content = htmlspecialchars_decode(str_replace('/uploads/', $url, $info->content));
            $info->content = str_replace('\\', '', $info->content);
            return Result::success('获取成功！', $info);
        } catch (\Exception $e) {

            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取列表
     */
    public function getList(): Response
    {
        try {
            $param = $this->request->get();
            $map[] = ['status', '=', 1];
            if (isset($param['cate_1']) && $param['cate_1']) {
                $map[] = ['cate_1', '=', $param['cate_1']];
            }
            $like = [];
            if (isset($param['keyword']) && $param['keyword']) {
                $like['field'] = 'title';
                $like['keyword'] = $param['keyword'];
            }
            if (isset($param['pick']) && $param['pick']) {
                $map[] = ['pick', '=', 1];
                if (!isset($param['cate_1'])) {
                    $map[] = ['cate_1', '<>', 1];
                }
            }
            $list = ProductModel::getPageList($map, 'id,title,price,vip_price,icon', $param['page'], 20, true, '', '', ['key' => 'sort', 'value' => 'asc'], $like);
            $token = $this->request->header('token');
            if ($token) {
                $user = User::where('token', $token)->find();
                if ($user) {
                    $money = $user->assets->integral;
                } else {
                    $money = 0;
                }
            } else {
                $money = 0;
            }
            $list['integral'] = $money;
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 获取规格价格
     */
    public function getSpecPrice($pid, $spec): Response
    {
        try {
            $specValue = ProductSpec::where(['pid' => $pid, 'merge_title' => $spec])->find();
            if (empty($specValue)) {
                return Result::fail('当前规格不存在！');
            }
            return Result::success('获取成功！', $specValue);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 商品结算
     */
    public function submit(): Response
    {
        try {
            $param = $this->request->post();
            $param['type'] = 1;
            return Result::fastReturn(OrderTemporary::setTemporary($param, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
